﻿using System.Collections;
using System.Net.Cache;
using System;
using UMC.Data;
using UMC.Net;
using UMC.Web;
using UMC.Web.UI;
// using System.Net;

namespace UMC.ITME
{
    [Apiumc("Http", "Client", Auth = WebAuthType.Admin, Desc = "在线终端")]
    class HttpClientActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {


            var BridgeNode = this.AsyncDialog("BridgeNode", r =>
            {
                var cleints = HttpsMimeSocket.ClientCache.Find(new HttpsMimeSocket.ClientInfo(), 0, 1500, out var _);

                UISection ui = UISection.Create(new UITitle("终端连接"));

                foreach (var o in cleints)
                {
                    var qty = BitConverter.ToInt32(o.Quantity.Span);//.ToString()o.Quantity
                    if (qty > 0)
                    {
                        ui.NewSection().AddCell("IP地址", new System.Net.IPAddress(o.IP.Span).ToString())
                        .AddCell("终端指纹", Utility.ToBase64String(o.Finger.Span))
                        .AddCell("在线连接", $"{BitConverter.ToInt32(o.Quantity.Span)}条")
                        .AddCell("终端攻击", $"{BitConverter.ToInt32(o.Error.Span)}次")
                        .AddCell("最后时间", Utility.TimeSpan(BitConverter.ToUInt32(o.Time.Span)).ToString());
                    }
                }
                if (ui.Count == 1)
                {
                    var desc = new UIDesc(new UMC.Web.WebMeta().Put("title", "暂无连接的终端").Put("icon", "\uea05"));
                    desc.Desc("{icon}\n{title}");
                    desc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                    ui.Add(desc);
                }
                ui.SendTo($"{request.Model}.{request.Command}", this.Context);

            }).Split('.');
            var ipValue = new StringValue(Utility.FromBase64String(BridgeNode[0]));
            var Finder = new StringValue(Utility.FromBase64String(BridgeNode[1]));
            var cleint = HttpsMimeSocket.ClientCache.Get(new HttpsMimeSocket.ClientInfo()
            {
                Finger = Finder,
                IP = ipValue
            });//, 0, 1500, out var _);
            if (cleint == null)
            {
                this.Prompt("未有此客户终端");
            }
            switch (this.AsyncDialog("Type", "Error"))
            {
                case "Stop":
                    this.AsyncDialog("Confirm", r => new UIConfirmDialog("确认终止此终端在线连接吗?"));
                    if (HttpMimeServier.Stop(ipValue.Span, Finder.Span) == 0)
                    {
                        BitConverter.TryWriteBytes(cleint.Quantity.Span, 0);
                    }
                    break;
                case "Pause":
                    this.AsyncDialog("Confirm", r => new UIConfirmDialog("确认暂停此终端20分钟连接吗?"));
                    BitConverter.TryWriteBytes(cleint.Error.Span, 100);

                    BitConverter.TryWriteBytes(cleint.Time.Span, Utility.TimeSpan());
                    break;
                default:
                    this.AsyncDialog("Confirm", r => new UIConfirmDialog("确认清零终端攻击数吗?"));
                    BitConverter.TryWriteBytes(cleint.Error.Span, 0);
                    break;
            }



        }
    }
}